package main.firefox;

import java.util.Date;
import java.util.TreeMap;



public class SortObj {

    private int bugID;
    private Date date;
    private TreeMap<Date, SortStatusResObj> dataOfSortStatus = new TreeMap<Date, SortStatusResObj>();

    private Date resolutionsDate;
    private Date anyResoDate;

    private String productId;
    private String compoenetId;

    private String lastResolution;
    private String reportId;

    public String getLastResolution() {
        return lastResolution;
    }

    public void setLastResolution(String lastResolution) {
        this.lastResolution = lastResolution;
    }

    public String getReportId() {
        return reportId;
    }

    public void setReportId(String reportId) {
        this.reportId = reportId;
    }

    public TreeMap<Date, SortStatusResObj> getDataOfSortStatus() {
        return dataOfSortStatus;
    }

    public void setDataOfSortStatus(TreeMap<Date, SortStatusResObj> dataOfSortStatus) {
        this.dataOfSortStatus = dataOfSortStatus;
    }

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public String getCompoenetId() {
        return compoenetId;
    }

    public void setCompoenetId(String compoenetId) {
        this.compoenetId = compoenetId;
    }

    public Date getAnyResoDate() {
        return anyResoDate;
    }

    public void setAnyResoDate(Date anyResoDate) {
        this.anyResoDate = anyResoDate;
    }

    public Date getResolutionsDate() {
        return resolutionsDate;
    }

    public void setResolutionsDate(Date resolutionsDate) {
        this.resolutionsDate = resolutionsDate;
    }

    public int getBugID() {
        return bugID;
    }

    public void setBugID(int bugID) {
        this.bugID = bugID;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public boolean ThisDateExists(Date date) {

        if (dataOfSortStatus.get(date) != null) {
            return true;
        }

        return false;

    }

    public void addElementActivity(Date date, String field, String value) {

        SortStatusResObj sortStatusResObj = null;
        if (ThisDateExists(date)) {
            sortStatusResObj = dataOfSortStatus.get(date);
        } else {
            sortStatusResObj = new SortStatusResObj();

        }

        if (field.equals("Status")) {

            sortStatusResObj.setStatus(value);

        } else if (field.equals("Resolution")) {

            sortStatusResObj.setResolution(value);

        } else {
            System.out.println("Can't map Status- Resolution");
            System.exit(0);

            //throw new Exception("Can map Status- Resolution");
        }

        dataOfSortStatus.put(date, sortStatusResObj);

    }

    public Date getCloseDateForThis(Date queryDate) {

        Date returnDate = null;

        System.out.println("-->>>>>>>>>>>>>>>>>>>>>>>>>>>");
        for (Date date : dataOfSortStatus.keySet()) {

            SortStatusResObj resObj = dataOfSortStatus.get(date);

            System.out.println("-->--------------> " + date);
            if (resObj.getStatus() != null && (resObj.getStatus().equals("CLOSED") || resObj.getStatus().equals("VERIFIED") || resObj.getStatus().equals("RESOLVED")) && date.before(queryDate)) {

                if (returnDate == null) {
                    returnDate = date;
                    if (resObj.getResolution() != null && !resObj.getResolution().equals("---") && resObj.getResolution().length() > 3) {
                        lastResolution = resObj.getResolution();
                    }
                } else // Update to the most recent one.
                 if (returnDate.before(date)) {
                        returnDate = date;
                        if (resObj.getResolution() != null && !resObj.getResolution().equals("---") && resObj.getResolution().length() > 3) {
                            lastResolution = resObj.getResolution();
                        }
                    }
            }

        }

        return returnDate;
    }

    public double returnTimeThreshold() {
        if (lastResolution == null) {
            lastResolution = "Default";
        }
        System.out.println("Last Resolution Value " + lastResolution);

        int caseValue = 0;
        if (lastResolution.equals("WONTFIX")) {

            caseValue = 1;
        } else if (lastResolution.equals("INVALID")) {
            caseValue = 2;
        } else if (lastResolution.equals("WORKSFORME")) {
            caseValue = 3;
        } else if (lastResolution.equals("FIXED")) {
            caseValue = 4;
        } else if (lastResolution.equals("DUPLICATE")) {
            caseValue = 5;
        }

        switch (caseValue) {

            case 1:
                return 18;
            case 2:
                return 18;
            case 3:
                return 18;
            case 4:
                return 18;

            case 5:
                return 30;
            default:
                return 18;

        }

    }

    public double returnTimeThreshold(double position[]) {
        if (lastResolution == null) {
            lastResolution = "Default";
        }
        System.out.println("Last Resolution Value " + lastResolution);

        int caseValue = 7;
        if (lastResolution.equals("WONTFIX")) {

            caseValue = 0;
        } else if (lastResolution.equals("INVALID")) {
            caseValue = 1;
        } else if (lastResolution.equals("WORKSFORME")) {
            caseValue = 2;
        } else if (lastResolution.equals("FIXED")) {
            caseValue = 3;
        } else if (lastResolution.equals("LATER")) {
            caseValue = 4;
        } else if (lastResolution.equals("REMIND")) {
            caseValue = 5;
        } else if (lastResolution.equals("DUPLICATE")) {
            caseValue = 6;
        }

//                    case "WONTFIX":
//                return position[0];
//            case "INVALID":
//                return position[1];
//            case "WORKSFORME":
//                return position[2];
//            case "FIXED":
//                return position[3];
//            case "LATER":
//                return position[4];
//            case "REMIND":
//                return position[5];
//                
//                
//
//
//            case "DUPLICATE":
////                System.exit(0);
//                return position[6];
//            default:
//                return position[7];
        switch (caseValue) {
            case 0:
                return position[0];
            case 1:
                return position[1];
            case 2:
                return position[2];
            case 3:
                return position[3];
            case 4:
                return position[4];
            case 5:
                return position[5];
            case 6:
                return position[6];
            default:
                return position[7];

        }

    }

    public boolean checkIfStillOpen(Date moment) {

//        System.out.println(dataOfSortStatus.lastKey());
//        
//        if (dataOfSortStatus == null || dataOfSortStatus.size() == 0) {
//            return true;
//        }
        SortStatusResObj resObj = null;
        Date prevD = null;
        for (Date d : dataOfSortStatus.keySet()) {

            if (dataOfSortStatus.get(d) != null && dataOfSortStatus.get(d).getStatus() != null) {
                if (d.before(moment)) {

                    if (prevD == null) {
                        resObj = dataOfSortStatus.get(d);
                        System.out.println("Am here dude" + d);
                    } else if (prevD.before(d)) {
                        resObj = dataOfSortStatus.get(d);
                        System.out.println("Am here in the else" + d);
                    }

                }
            }
            prevD = d;
        }

        if ((resObj != null && resObj.getStatus() != null && (resObj.getStatus().equals("RESOLVED") || resObj.getStatus().equals("CLOSED") || resObj.getStatus().equals("VERIFIED")))) {

            System.out.println("Am here koko" + prevD + "   " + resObj.getResolution() + "  ," + resObj.getStatus());
            return false;

        }

        return true;

    }

}
